package servlet;

import dao.UserDao;
import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/OrderUserServlet.do")
public class OrderUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(username);

        // 使用UserDao根据username获取user_id
        UserDao userDao = new UserDao();
        try {
            int userId = userDao.getUserIdByUsername(username);
            List<Map<String, Object>> searchResults = performQueryById("SELECT * FROM `order` WHERE user_id = ?", userId);

            session.setAttribute("searchResults", searchResults);
            response.sendRedirect(request.getContextPath() + "/background1/user/OrderView.jsp");
        } catch (Exception e) {
            e.printStackTrace(); // Replace with appropriate error handling
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occurred while processing your request.");
        }
    }

    private List<Map<String, Object>> performQueryById(String sql, int userId) {
        List<Map<String, Object>> orderList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = null;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, userId);

            while (resultSet != null && resultSet.next()) {
                int orderId = resultSet.getInt("order_id");
                String recipientName = resultSet.getString("recipient_name");
                String recipientAddress = resultSet.getString("recipient_address");
                String recipientPhone = resultSet.getString("recipient_phone");
                String notes = resultSet.getString("notes");
                String orderDate = resultSet.getString("order_date");
                String orderStatus = resultSet.getString("order_status");
                double orderTotal = resultSet.getDouble("order_total");

                Map<String, Object> order = new HashMap<>();
                order.put("order_id", orderId);
                order.put("user_id", userId);
                order.put("recipient_name", recipientName);
                order.put("recipient_address", recipientAddress);
                order.put("recipient_phone", recipientPhone);
                order.put("notes", notes);
                order.put("order_date", orderDate);
                order.put("order_status", orderStatus);
                order.put("order_total", orderTotal);

                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return orderList;
    }
}
